<?php
include('../data/comm.inc.php');
include('../data/myadminvar.php');
include('../func/func.php');
include('../func/csfunc.php');
include('../func/adminfunc.php');
include('../include.php');
include('./checklogin.php');

switch ($_REQUEST['xtype']) {
    case "show":
        $msql->query("select ifhide,xpage,pagename from `$tb_admins_page` where adminid=10000 order by sortx");
		$xpage = array();
		$i=0;
        while ($msql->next_record()) {
			$xpage[$i]['xpage'] = $msql->f('xpage');
            $xpage[$i]['ifhide'] = $msql->f('ifhide');
			$xpage[$i]['pagename'] = $msql->f('pagename');
			$i++;
        }
        $tpl->assign("xpage",$xpage);
		
        $msql->query("select * from `$tb_ctrl`");
        $msql->next_record();
        $tpl->assign("autoopenpan", $msql->f('autoopenpan'));
        $tpl->assign("autoopenpantime", $msql->f('autoopenpantime'));
        $tpl->assign("autofly", $msql->f('autofly'));
        $tpl->assign("autoflytime", $msql->f('autoflytime'));
        $tpl->assign("kjjs", $msql->f('kjjs'));
        $tpl->assign("kjjstime",$msql->f('kjjstime'));
        //$tpl->display("check.html");
		//exit;
        include("../data/cuncu.php");
        $kksql->query("show triggers");
        $tg = array();
        while ($kksql->next_record()) {
            $tg[] = $kksql->f('Trigger');
        }
        if (in_array('updatelib', $tg)) {
            $updatelib = 1;
        }
        if (in_array('deletelib', $tg)) {
            $deletelib = 1;
        }
        if (in_array('updateplay', $tg)) {
            $updateplay = 1;
        }
        $tpl->assign("updatelib", $updatelib);
        $tpl->assign("deletelib", $deletelib);
        $tpl->assign("updateplay", $updateplay);
        
        $msql->query("select count(id) from `$tb_error`");
        $msql->next_record();
        $tpl->assign("liberrnum", $msql->f(0));
        $bigdata = 0;
        $msql->query("SHOW TABLE STATUS LIKE '$tb_lib'");
        $msql->next_record();
        if ($msql->f('Engine') == 'MRG_MYISAM') {
            $bigdata = 1;
        }
        $tpl->assign("bigdata", $bigdata);
        if ($bigdata == 1) {
            $msql->query("select lib from `$tb_config`");
            $msql->next_record();
            $lib = $msql->f('lib');
        }
        $mrg = array();
        $arr = $msql->arr("SHOW TABLES LIKE 'x\_lib%'", 0);
        $i   = 0;
        
        foreach ($arr as $k => $v) {
            if ($v[0] != 'x_lib_err') {
                $mrg[$i]['tb'] = $v[0];
                $msql->query("select count(id) from `" . $v[0] . "`");
                $msql->next_record();
                $mrg[$i]['num'] = $msql->f(0);
                $mrg[$i]['in']  = 0;
                
                if (strpos($lib, $v[0]) > 0 & $v[0] != 'x_lib') {
                    $mrg[$i]['in'] = 1;
                }
                $i++;
            }
        }
        $tpl->assign("mrg", $mrg);
        $tpl->display("check.html");
        break;
    case "qkerr":
        $msql->query("delete from `$tb_error`");
        echo 1;
        break;
    case "mrg":
        if ($_POST['pass'] != '2403088807') {
            echo 2;
            exit;
        }
        include("../data/cuncu.php");
        $ac = $_POST['ac'];
        if ($ac == 'sc') {
            $kksql->query("create table if not exists `x_lib_old` like `x_lib`");
            $kksql->query("delete from `x_lib_old` where 1");
            $kksql->query("insert into `x_lib_old` select * from `x_lib`");
            $ymd = date("Ymd");
            $kksql->query("create table if not exists `x_lib_$ymd` like `x_lib`");
            $kksql->query("delete from `x_lib_$ymd` where 1");
            $kksql->query("ALTER TABLE `x_lib`  ENGINE = MRG_MYISAM,INSERT_METHOD=LAST");
            $kksql->query("alter table `x_lib` UNION=(`x_lib_old`,`x_lib_$ymd`)");
            $kksql->query("FLUSH TABLE `x_lib` ");
            $lib = "`x_lib_old`,`x_lib_$ymd`";
            $kksql->query("update `$tb_config` set lib='$lib'");
        } else if ($ac == 'hf') {
            $kksql->query("create table if not exists `x_lib_tmp` like `x_lib`");
            $kksql->query("ALTER TABLE `x_lib_tmp`  ENGINE = MYISAM");
            $kksql->query("DELETE from `x_lib_tmp` where 1");
            $kksql->query("insert into `x_lib_tmp` select * from `x_lib`");
            $kksql->query("drop tables  if exists `x_lib`");
            $kksql->query("alter table `x_lib_tmp` rename `x_lib`");
            $kksql->query("update `$tb_config` set lib=''");
        } else if ($ac == 'resc') {
            $tb    = explode('|', $_POST['tb']);
            $str   = '';
            $ymd   = date("Ymd");
            $newtb = "x_lib_" . $ymd;
            foreach ($tb as $key => $val) {
                if (strpos("#" . $val, "x_lib_") > 0 & $val != $newtb) {
                    $kksql->query("create table if not exists `$val` like `x_lib`");
                    $kksql->query("ALTER TABLE `$val`  ENGINE = MYISAM");
                    if ($str != '') {
                        $str .= ",";
                    }
                    $str .= "`" . $val . "`";
                }
            }
            
            if ($str != '') {
                $kksql->query("create table if not exists `$val` like `$newtb`");
                $kksql->query("ALTER TABLE `$val`  ENGINE = MYISAM");
                $str .= ",`" . $newtb . "`";
                $kksql->query("alter table x_lib union=($str)");
                $kksql->query("FLUSH TABLE `x_lib` ");
                $kksql->query("update `$tb_config` set lib='$str'");
            }
        } else if ($ac == 'scxz') {
            $bigdata = 0;
            $msql->query("SHOW TABLE STATUS LIKE '$tb_lib'");
            $msql->next_record();
            if ($msql->f('Engine') == 'MRG_MYISAM') {
                $bigdata = 1;
            }
            if ($bigdata == 1) {
                $msql->query("select lib from `$tb_config`");
                $msql->next_record();
                $lib = $msql->f('lib');
                
                $tb  = explode('|', $_POST['tb']);
                $str = '';
                foreach ($tb as $key => $val) {
                    if (strpos($lib, $val) <= 0 & $val != 'x_lib' & $val != 'x_libu' & $val!='') {
                        $kksql->query("drop table if exists `$val` ");
                    }
                }
            } else {
                
                $tb  = explode('|', $_POST['tb']);
                $str = '';
                foreach ($tb as $key => $val) {
                    if ($val != 'x_lib' & $val != 'x_libu' & $val!='') {
                        $kksql->query("drop table if exists `$val` ");
                    }
                }
            }
        }
        echo 1;
        
        break;
    case "tg":
        $ac   = $_POST['ac'];
        $name = $_POST['name'];
        include("../data/cuncu.php");
        $updateplaydel = "DROP TRIGGER IF EXISTS `updateplay` ";
        $updateplayadd = "CREATE TRIGGER `updateplay` BEFORE UPDATE ON `x_play` FOR EACH ROW BEGIN  if new.peilv1>new.mp1  then set new.peilv1=new.mp1; end if; end;";
        if ($name == 'ulib') {
            if ($ac == 'add') {
                $kksql->query($updatecc);
            } else {
                $kksql->query($updatestr);
            }
        }
        if ($name == 'dlib') {
            if ($ac == 'add') {
                $kksql->query($deletecc);
            } else {
                $kksql->query($deletestr);
            }
        }
        if ($name == 'uplay') {
            if ($ac == 'add') {
                $kksql->query($updateplayadd);
            } else {
                $kksql->query($updateplaydel);
            }
        }
        
        echo 1;
        break;
    case "upgn":
		$msql->query("select ifhide,xpage,pagename from `$tb_admins_page` where adminid=10000 order by sortx");
		while($msql->next_record()){
			$tmp = $msql->f('xpage');
			if(trim($_POST[$tmp])==1){
               $fsql->query("update `$tb_admins_page` set ifhide=0,ifok=1 where adminid=10000 and xpage='".$tmp."'");
               $fsql->query("update `$tb_admins_page` set ifhide=0,ifok=0 where adminid!=10000 and xpage='".$tmp."'");
			}else{
			   $fsql->query("update `$tb_admins_page` set ifhide=1,ifok=0 where xpage='".$tmp."'");
			}
		}

        echo 1;
        
        break;
    case "setctrl":
        $autoopenpan = $_POST['autoopenpan'];
        $autofly     = $_POST['autofly'];
        $kjjs        = $_POST['kjjs'];
        $time        = time();
        $msql->query("update `$tb_ctrl` set autoopenpan='$autoopenpan',autoopenpantime=NOW(),autofly='$autofly',autoflytime=NOW(),kjjs='$kjjs',kjjstime=NOW(),slowautoopenpan='$slowautoopenpan',slowautoopenpantime=NOW(),slowautofly='$slowautofly',slowautoflytime=NOW(),slowkjjs='$slowkjjs',slowkjjstime=NOW()");
        echo 1;
        break;
}
?>